package com.cneiu.ppms.web.action;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.cneiu.ppms.web.base.BaseController;
import com.cneiu.ppms.web.model.OrganizationModel;
import com.cneiu.ppms.web.service.OrganizationService;
import com.jfinal.plugin.activerecord.Page;

/**
 * 机构的控制器
 * 
 * @author zhaokf
 * @date 2013-04-15
 */
public class OrganizationController extends BaseController
{
	/**
	 * 获取机构列表（ComboBox树形）
	 */
	public void treeComboBox(){
		/**根据前台传递过来的ID查询根节点**/
		String node = getPara("node");
		OrganizationModel model =OrganizationModel.dao.findFirst("select * from sa_org where org_no = ?", node);
		List<HashMap<Object,Object>> mapList =  null;
		/**可能存在多个根节点情况，获取每个根节点的子节点（递归）**/
		mapList =OrganizationService.treeComboBox(model);
		/**判断并返回数据**/
		if (null != mapList && mapList.size()>0){
			renderGrid(mapList);
		}else{
			renderAjax(false, OrganizationService.getError());
		}
	}
	
	
	
	/**
	 * 获取机构列表（树形）
	 */
	public void treeList(){
		/**根据前台传递过来的ID查询根节点**/
		String node = getPara("node");
		List<OrganizationModel> modelList = OrganizationModel.dao.find("select * from sa_org where org_no = ?", node);
		/**可能存在多个根节点情况，获取每个根节点的子节点（递归）**/
		List<HashMap<Object,Object>> mapList = new ArrayList<HashMap<Object,Object>>();
		for(OrganizationModel model:modelList){
			mapList.add(OrganizationService.treeList(model));
		}
		/**判断并返回数据**/
		if (null != mapList && mapList.size()>0){
			renderGrid(mapList);
		}else{
			renderAjax(false, OrganizationService.getError());
		}
	}
	/**
	 * 获取page limit得到机构列表
	 */
	public void list()
	{
		Page<OrganizationModel> Orgs = OrganizationService.list(this.getParams());
		if (null != Orgs)
		{
			renderGrid(Orgs.getTotalRow(), Orgs.getList());
		}
		else
		{
			renderAjax(false, OrganizationService.getError());
		}
	}

	/**
	 * 添加机构
	 */
	public void add()
	{
		try
		{
			boolean isNotError = true;
			if (!OrganizationService.isAdd(this.getParams()))
			{
				isNotError = false;
			}
			renderAjax(isNotError, OrganizationService.getError());
		}
		catch (Exception e)
		{
		}
	}

	/**
	 * 编辑机构
	 */
	public void edit()
	{
		try
		{
			boolean isNotError = true;
			if (OrganizationService.isUpdate(this.getParams()))
			{
				isNotError = false;
			}
			renderAjax(isNotError, OrganizationService.getError());
		}
		catch (Exception e)
		{
		}
	}

	/**
	 * 删除机构
	 */
	public void delete()
	{
		try
		{
			boolean isNotError = true;
			if (!OrganizationService.isDelete(this.getParams()))
			{
				isNotError = false;
			}
			renderAjax(isNotError, OrganizationService.getError());
		}
		catch (Exception e)
		{
		}
	}
}
